<template>
  <el-dialog
    class="add-org-comp"
    title="编辑"
    :close-on-click-modal="false"
    :append-to-body="true"
    :destroy-on-close="true"
    :visible="visible"
    :width="$setRem(450)"
    :before-close="handleCancel">
    <el-form
      :model="ruleForm"
      :rules="rules"
      size="medium"
      ref="ruleForm"
      :label-width="$setRem(120)"
      class="rule-form"
    >

      <el-form-item
        label="机构名称"
        prop="orgName"
        required>
        <el-input
          clearable
          placeholder="请输入机构名称"
          v-model="ruleForm.orgName">
        </el-input>
      </el-form-item>
      <el-form-item
        label="机构简称"
        prop="orgAbbr">
        <el-input
          clearable
          placeholder="请输入机构简称"
          v-model="ruleForm.orgAbbr">
        </el-input>
      </el-form-item>
      <el-form-item
        label="机构简介"
        prop="orgIntro">
        <el-input
          clearable
          type="textarea"
          :rows="3"
          placeholder="请输入机构简介"
          v-model="ruleForm.orgIntro">
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        size="medium"
        @click="handleCancel">取 消</el-button>
      <el-button
        size="medium"
        :loading="loading"
        type="primary"
        @click="hanldeSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 弹框是否展示
    visible: {
      type: Boolean,
      default: true
    },
    loading: {
      type: Boolean,
      default: false
    },
    // 回填数据
    options: {
      type: Object,
      default: () => ({})
    },
  },
  watch: {
    options: {
      deep: true,
      handler(data) {
        if (data) {
          const keys = Object.keys(this.ruleForm);
          Object.keys(data).length && keys.forEach(key => {
            this.ruleForm[key] = data[key] || '';
          });
        }
      }
    },
    visible(val) {
      if (!val) {
        this.ruleForm = {
          id: '',
          orgType: '',
          orgName: '',
          orgAbbr: '',
          orgIntro: ''
        };
      }
    },
    projectId(val) {
      if (val) {
        this.getRoleList();
      }
    }
  },
  data() {
    return {
      roleList: [], // 角色信息
      ruleForm: {
        id: '',
        orgType: '',
        orgName: '',
        orgAbbr: '',
        orgIntro: ''
      },
      rules: {
        orgName: [
          { required: true, message: '请输入机构名称' }
        ],
      }
    };
  },
  methods: {
    handleCancel() {
      this.$emit('cancel', true);
    },
    hanldeSave() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.$emit('submit', this.ruleForm);
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
  @import "../../assets/styles/dialog";

  .add-org-comp {
    /deep/ .el-form-item {
      .el-form-item__content {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        > .el-input, .el-select, .el-textarea {
          width: calc(100% - 30px);
        }
      }
    }
  }
</style>
